package com.kins.so.chatassistant.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jakarta.annotation.Generated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/26 12:17
 */
@Component
public class CommonUtils {
    @Generated
    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);

    @Autowired(
        required = false
    )
    private CacheClient cacheClient;

    public CommonUtils(){
    }

    public static String getUUID(){ return UUID.randomUUID().toString().replace("-", ""); }

    public String getCurrLoginUser(){ return SysUtils.toUserEntity(this.getUser());}

    public Map<String,Object> getUser(){
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            Map<String,Object> curUserMap = this.getUser(request);
            return curUserMap;
        }catch(Exception var3){
            return HashMap();
        }
    }

    private Map<String,Object> getUser(HttpServletRequest request){
        Map<String,Object> curUserMap = null;
        String redisId = request.getHeader(SystemConst.USER_ID);
        if (!StringUtils.isEmpty(redisId)){
            curUserMap = this.getSessionUser(redisId,request);
        }
        return curUserMap;
    }

    private Map<String,Object> getSessionUser(String redisId,HttpServletRequest request){
        Map<String,Object> userMap = null;
        curUserMap = (Map)this.cacheClient.get(redisId);
        log.debug(String.format("CommonUtils >>> getSessionUser >>> redisId: %s, userMap: %s", curUserMap));
        return curUserMap;
    }
    
    private void delUser(HttpServletRequest request){
        String redisId = request.getHeader(SystemConst.USER_ID);
        if (!StringUtils.isEmpty(redisId)){
            this.cacheClient.del(redisId);
        }
    }

    private String saveUser(String username,Map<String,Object> userMap,int timeout){
        String redisId = this.getRedisId(username);
        this.cacheClient.set(redisId,userInfo,(long)timeOut);
        return redisId;
    }

    private String getRedisId(String username){
        String uuid = UUID.randomUUID().toString()
        long t = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        StringBuffer redisId = s.append(username).append(":").append(uuid)
        return redisId.toString();
    }

    public static Object getFieldValueByName(String fieldName,Object o){
        if (fieldName != null || o != null){
            try{
                String firstLetter = fieldName.substring(0,1).toUpperCase();
                String getter = "get" + firstLetter + fieldName.substring(1);
                Method method = o.getClass().getMethod(getter);
                Object value = method.invoke(o);
                return value;
            }catch(Exception e){
                return null;
            }
        }else {
            log.error("不得上传空值");
            return null;
        }
    }

    public static String humpToLine(String str){
        String var10000 = str.substring(0,1).toLowerCase();
        str = var10000 + str.substring(1);
        Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()){
            matcher.appendReplacement(sb,"_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String getEntityAttribute(Object o){
        String attribute = "";
        Field[] fields = SysUser.class.getDeclaredFields();
        Field[] var3 = fields;
        int var4 = fields.length;

        for(int var5 = 0; var5 < var4; ++var5){
            Field f = var3[var5];
            attribute = attribute + f.getName() + ",";
        return attribute;
    }


}
